<?php

namespace app\common\model;

use app\index\model\Question;
use think\Model;

class Api extends Model
{
    public function up($data){
        if($this->save($data,['Id'=>1])){
            return true;
        }
        return false;
    }
    public function get_Token(){
        return $this->where('Id',1)->find()['token'];
    }
    //用户查看答案操作
    public function get_ask($que_id){
        $u = new User();
        $user = $u->getinfo();
        $q = new Question();
        $question = $q->getone($que_id);
        if($question['price'] < 1){
            //免费题目 直接返回答案
            return $question['answer'];
        }
        //如果不是免费题目，检测用户是否是vip
        if($user['grade'] == 1){
            //vip 直接返回答案
            return $question['answer'];
        }
        //不是vip 检测余额是否足够
        if($user['balance'] < $question['price']){
            return '账号余额不足';
        }
        //扣除余额 之后返回答案
        $user->setDec('balance',$question['price']);
        return $question['answer'];
    }
}
